//
//  merchantList.m
//  cityStar
//
//  Created by joyban on 14-12-24.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "merchantList.h"
#import "merchantListCell.h"
#import "HWSearchViewController.h"

#import "StayViewController.h"
#import "RepairAndHomeViewController.h"
#import "CommunityDetailViewController.h"
#import "FSDropDownMenu.h"
#import "SingleTableView.h"
#import "UIColor+FlatUI.h"

NSString *const merchantListTableViewCellID = @"merchantListCellID";


@interface merchantList ()<SingleTableViewDelegate,FSDropDownMenuDelegate,FSDropDownMenuDataSource>
@property (weak, nonatomic) IBOutlet UITableView *merchantListTable;
@property (weak, nonatomic) IBOutlet UIButton *AllBusinessdistrictbtn;
@property (weak, nonatomic) IBOutlet UIButton *AlltypeBtn;
@property (weak, nonatomic) IBOutlet UIButton *Intelligencebtn;

@property(nonatomic,strong) NSArray *cityArr;
@property(nonatomic,strong) NSArray *areaArr;
@property(nonatomic,strong) NSArray *currentAreaArr;
@property (nonatomic,weak) IBOutlet UIImageView *addressImageView;
@property (nonatomic, strong) FSDropDownMenu *menu;
@property (nonatomic, assign) BOOL isShowAddressList;

@property (nonatomic, weak) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIImageView *typeImageView;
@property (strong, nonatomic) SingleTableView *typeSingleTableView;

@property (nonatomic, weak) IBOutlet UIImageView *noopsycheImageView;
@property (strong, nonatomic) SingleTableView *noopsycheSingleTableView;


@end

@implementation merchantList

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商户列表";
    [self addBackButton];
    [self setUpTableViews];
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithImage:[UIImage imageNamed:@"bsearch" ] actionBlock:^{
        HWSearchViewController *hwSearchViewController = [[HWSearchViewController alloc]init];
        hwSearchViewController.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:hwSearchViewController animated:YES];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAddressStatues) name:@"isShowAddressList" object:nil];
    
    self.cityArr = @[@"附近",@"上海",@"北京",@"同城"];
    self.areaArr = @[
                     @[@"附近",@"500米",@"1000米",@"2000米",@"5000米"],
                     @[@"徐家汇",@"人民广场",@"陆家嘴"],
                     @[@"三里屯",@"亚运村",@"朝阳公园"],
                     @[@"同城"],
                     ];
    self.currentAreaArr = _areaArr[0];
    FSDropDownMenu *menu = [[FSDropDownMenu alloc] initWithOrigin:CGPointMake(0, 36) andHeight:300];
    menu.transformView = self.addressImageView;
    menu.tag = 1001;
    menu.dataSource = self;
    menu.delegate = self;
    [self.view addSubview:menu];
    
}

- (void)addRightButtonWithImage:(UIImage *)image actionBlock:(void(^)(void))block
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 20, 20)];
    [rightButton setImage:image forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setImage:[UIImage imageNamed:@"bsearch_touch"] forState:UIControlStateHighlighted];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.rightBarActionBlock = block;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

-(void)changeAddressStatues
{
    self.isShowAddressList = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpTableViews
{
    [self.merchantListTable registerNib:[merchantListCell nib] forCellReuseIdentifier:merchantListTableViewCellID];
}
#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [merchantListCell heightOfTableCell];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    merchantListCell *ListdetailCell = [tableView dequeueReusableCellWithIdentifier:merchantListTableViewCellID];
    ListdetailCell.selectionStyle = UITableViewCellSelectionStyleGray;
    return ListdetailCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
    RepairAndHomeViewController *HomeController = [[RepairAndHomeViewController alloc]init];
    [self.navigationController pushViewController:HomeController animated:YES];
}

#pragma mark - Actions
-(IBAction)allTypes_atction:(id)sender
{
    if (self.isShowAddressList == YES) {
        [self.menu menuTapped];
        self.isShowAddressList = NO;
    }
    
    if(self.AlltypeBtn.selected == NO)
    {
        self.AlltypeBtn.selected = YES;
    }else
    {
        self.AlltypeBtn.selected = NO;
    }
    self.AllBusinessdistrictbtn.selected = NO;
    self.Intelligencebtn.selected = NO;
    
    [self.typeSingleTableView showFromPosition:CGPointMake(0, 25) inViewController:self dataType:SingleTableViewDataTypeSortModel];
    self.typeSingleTableView.dataArray = @[@"全部分类",@"餐饮类",@"便利配套类",@"食品类",@"酒店类",@"休闲娱乐类",@"百货商品类"];
}

-(IBAction)Intelligence_atction:(id)sender
{
    if (self.isShowAddressList == YES) {
        [self.menu menuTapped];
        self.isShowAddressList = NO;
    }
    if(self.Intelligencebtn.selected == NO)
    {
        self.Intelligencebtn.selected = YES;
    }else
    {
        self.Intelligencebtn.selected = NO;
    }
    self.AlltypeBtn.selected = NO;
    self.AllBusinessdistrictbtn.selected = NO;
    
    [self.noopsycheSingleTableView showFromPosition:CGPointMake(0, 25) inViewController:self dataType:SingleTableViewDataTypeSortModel];
    self.noopsycheSingleTableView.dataArray =@[@"智能排序",@"价格从低到高",@"价格从高到低",@"评论排序"];
}

-(IBAction)AllBusinessdistrictbtn_action:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:TableViewWillShowNotification object:self];

    if(self.AllBusinessdistrictbtn.selected == NO)
    {
        self.AllBusinessdistrictbtn.selected = YES;
    }else
    {
        self.AllBusinessdistrictbtn.selected = NO;
    }
    self.AlltypeBtn.selected = NO;
    self.Intelligencebtn.selected = NO;
    self.menu = (FSDropDownMenu*)[self.view viewWithTag:1001];
    [UIView animateWithDuration:0.2 animations:^{
        
    } completion:^(BOOL finished) {
        [self.menu menuTapped];

        if (self.isShowAddressList == YES) {
            self.isShowAddressList = NO;
            self.AllBusinessdistrictbtn.selected = NO;
            
        }else{
            self.isShowAddressList = YES;
            self.AllBusinessdistrictbtn.selected = YES;
            
        }
    }];

}

#pragma mark - singleTableViewDelegate
- (SingleTableView *)typeSingleTableView
{
    if (!_typeSingleTableView) {
        _typeSingleTableView = [[SingleTableView alloc] init];
        _typeSingleTableView.delegate = self;
        [self addChildViewController:_typeSingleTableView];
        [self.view addSubview:_typeSingleTableView.view];
        [self.view bringSubviewToFront:self.topView];
        _typeSingleTableView.arrawImageView = self.typeImageView;
    }
    return _typeSingleTableView;
}

- (void)singleTableView:(SingleTableView *)tableView didSelectedCellAtIndexPath:(NSIndexPath *)indexPath
{
    self.AlltypeBtn.selected = NO;
    self.AllBusinessdistrictbtn.selected = NO;
    self.Intelligencebtn.selected = NO;
    if (tableView == self.typeSingleTableView) {
        NSString *celltest = @[@"全部分类",@"餐饮类",@"便利配套类",@"食品类",@"酒店类",@"休闲娱乐类",@"百货商品类"][indexPath.row];
        if (celltest.length >= 5) {
            NSString *btnTitle = [NSString stringWithFormat:@"%@..",[celltest substringWithRange:NSMakeRange(0, 4)]];
            ;
            [self.AlltypeBtn setTitle:btnTitle forState:UIControlStateNormal];
        }else{
        [self.AlltypeBtn setTitle:celltest forState:UIControlStateNormal];
        }
    }else
    {
        NSString *celltest =@[@"智能排序",@"价格从低到高",@"价格从高到低",@"评论排序"][indexPath.row];
        if (celltest.length >= 5) {
            NSString *btnTitle = [NSString stringWithFormat:@"%@..",[celltest substringWithRange:NSMakeRange(0, 4)]];
            ;
            [self.Intelligencebtn setTitle:btnTitle forState:UIControlStateNormal];
        }else{
            [self.Intelligencebtn setTitle:celltest forState:UIControlStateNormal];
        }
    }
    
}

- (SingleTableView *)noopsycheSingleTableView
{
    if (!_noopsycheSingleTableView) {
        _noopsycheSingleTableView = [[SingleTableView alloc] init];
        _noopsycheSingleTableView.delegate = self;
        [self addChildViewController:_noopsycheSingleTableView];
        [self.view addSubview:_noopsycheSingleTableView.view];
        [self.view bringSubviewToFront:self.topView];
        _noopsycheSingleTableView.arrawImageView = self.noopsycheImageView;
    }
    return _noopsycheSingleTableView;
}
#pragma mark - reset button size

-(void)resetItemSizeBy:(NSString*)str{
    if (str.length >= 5) {
        NSString *btnTitle = [NSString stringWithFormat:@"%@..",[str substringWithRange:NSMakeRange(0, 4)]];
        ;
        [self.AllBusinessdistrictbtn setTitle:btnTitle forState:UIControlStateNormal];
    }else{
        [self.AllBusinessdistrictbtn setTitle:str forState:UIControlStateNormal];
    }
}


#pragma mark - FSDropDown datasource & delegate

- (NSInteger)menu:(FSDropDownMenu *)menu tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == menu.rightTableView) {
        return _cityArr.count;
    }else{
        return _currentAreaArr.count;
    }
}
- (NSString *)menu:(FSDropDownMenu *)menu tableView:(UITableView*)tableView titleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == menu.rightTableView) {
        
        return _cityArr[indexPath.row];
    }else{
        return _currentAreaArr[indexPath.row];
    }
}

- (void)menu:(FSDropDownMenu *)menu tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(tableView == menu.rightTableView){
        _currentAreaArr = _areaArr[indexPath.row];
        [menu.leftTableView reloadData];
    }else{
        self.isShowAddressList = NO;
        self.AlltypeBtn.selected = NO;
        self.AllBusinessdistrictbtn.selected = NO;
        self.Intelligencebtn.selected = NO;
        [self resetItemSizeBy:_currentAreaArr[indexPath.row]];
    }
}

-(void)cancleTabelView
{
    self.AlltypeBtn.selected = NO;
    self.AllBusinessdistrictbtn.selected = NO;
    self.Intelligencebtn.selected = NO;
}

-(void)senderCancle
{
    self.AlltypeBtn.selected = NO;
    self.AllBusinessdistrictbtn.selected = NO;
    self.Intelligencebtn.selected = NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
